package com.brave.edu.manager.controller;

import com.brave.edu.manager.domian.Student;
import com.brave.edu.manager.service.StudentService;

import java.util.Scanner;

public class StudentContoller {
    StudentService StudentService = new StudentService();
    Scanner S = new Scanner(System.in);

    public void start() {

        student:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String select = S.next();
            switch (select) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用此系统，再见");
                    break student;
                default:
                    System.out.println("您输入得有误，请重新输入");
                    break;
            }
        }


    }

    private void updateStudent() {
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);
        StudentService.updateleStudent(updateId,newStu);
        System.out.println("修改成功");
    }

    private void deleteStudent() {
        String delId = inputStudentId();
        StudentService.deleteStudentId(delId);
        System.out.println("删除成功！");
    }

    private String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生id：");
            id = S.next();
            boolean exists = StudentService.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入！");
            } else {
                break;
            }
        }
        return id;
    }

    private void findAllStudent() {
        Student[] stus = StudentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t\t" + stu.getName() + "\t\t" + stu.getAge() + "\t\t" + stu.getSex());
            }
        }
    }

    private void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = S.next();
            boolean result = StudentService.isExists(id);
            if (result) {
                System.out.println("该学号被占用，请重新输入：");
            } else {
                break;
            }
        }
        Student stu = inputStudentInfo(id);

        boolean res = StudentService.addStudent(stu);
        if (res) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名：");
        String name = S.next();
        System.out.println("请输入学生年龄：");
        String age = S.next();
        System.out.println("请输入学生性别：");
        String sex = S.next();
        Student stu = new Student(id, name, age, sex);
        return stu;

    }
}
